// (c) 2011 John P. Costella.
using System; using System.Drawing; using System.Drawing.Imaging;
namespace Costella
{
    public static class BitmapUtils
    {
        public static Int2 Dimensions(this Bitmap b) {return new Int2(b.Width, b.Height);}
        public static void ForAll(this Bitmap b, Action<Int2> a) {for (var j = 0; j < b.Height; j++) for (var i = 0; i < b.Width; i++) a(new Int2(i, j));}
        public static Color Get(this Bitmap b, int i, int j) {return b.GetPixel(i, j);}
        public static Color Get(this Bitmap b, Int2 i) {return b.Get(i.I, i.J);}
        public static void Set(this Bitmap b, int i, int j, Color c) {b.SetPixel(i, j, c);}
        public static void Set(this Bitmap b, Int2 i, Color c) {b.Set(i.I, i.J, c);}
        public static void SmartSave(this Bitmap b, string f) {var g = f.ToLower(); b.Save(f, g.EndsWith(".jpg") ? ImageFormat.Jpeg : g.EndsWith(".bmp") ? ImageFormat.Bmp : ImageFormat.Png);}
    }
    public static class BitmapMagic
    {
        public static Bitmap Upsample(this Bitmap b) { return b.ToPixmap().Upsample().ToBitmap(); }
        public static Bitmap Upsample(this Bitmap b, int n) { var p = b.ToPixmap(); for (var i = 0; i < n; i++) p = p.Upsample(); return p.ToBitmap(); }
        public static Bitmap Downsample(this Bitmap b) { return b.ToPixmap().Downsample().ToBitmap(); }
        public static Bitmap Downsample(this Bitmap b, int n) { var p = b.ToPixmap(); for (var i = 0; i < n; i++) p = p.Downsample(); return p.ToBitmap(); }
        public static Bitmap Resample(this Bitmap b, double f) { return b.ToDPixmap().Resample(f).ToBitmap(); }
        public static Bitmap Resample(this Bitmap b, double xf, double yf) { return b.ToDPixmap().Resample(xf, yf).ToBitmap(); }
        public static Bitmap UpsampleX(this Bitmap b) { return b.ToPixmap().UpsampleX().ToBitmap(); }
        public static Bitmap UpsampleY(this Bitmap b) { return b.ToPixmap().UpsampleY().ToBitmap(); }
        public static Bitmap DownsampleX(this Bitmap b) { return b.ToPixmap().DownsampleX().ToBitmap(); }
        public static Bitmap DownsampleY(this Bitmap b) { return b.ToPixmap().DownsampleY().ToBitmap(); }
        public static Bitmap ResampleX(this Bitmap b, double f) { return b.ToDPixmap().ResampleX(f).ToBitmap(); }
        public static Bitmap ResampleY(this Bitmap b, double f) { return b.ToDPixmap().ResampleY(f).ToBitmap(); }
        static BitmapMagic()
        {
            Magic<Color>.C13 = (a, b) => Color.FromArgb(Magic<int>.C13(a.A, b.A), Magic<int>.C13(a.R, b.R), Magic<int>.C13(a.G, b.G), Magic<int>.C13(a.B, b.B));
            Magic<Color>.C1331 = (a, b, c, d) => Color.FromArgb(Magic<int>.C1331(a.A, b.A, c.A, d.A), Magic<int>.C1331(a.R, b.R, c.R, d.R), Magic<int>.C1331(a.G, b.G, c.G, d.G), Magic<int>.C1331(a.B, b.B, c.B, d.B));
            Magic<Double4>.Zero = new Double4(0, 0, 0, 0); Magic<Double4>.MultAdd = (a, b, c) => a * b + c;
        }
        public static void Init() { }
    }
}
